<?php
class AdminController extends Zend_Controller_Action
{
    public function init()
    {
        $auth = Zend_Auth::getInstance ();
        if ($auth->hasIdentity ()) {
            $this->view->identity = $auth->getIdentity ();
        }
        
        $this->_helper->layout->setLayout ( 'admin' );
    }

    public function homeAction()
    {
        $adminForm = new Form_Admin();
        $adminForm->setAction ( $this->_helper->url ( 'home', 'admin' ) );
        $adminForm->getElement ( 'username' )->setRequired ( FALSE );
        $adminForm->getElement ( 'password' )->setRequired ( FALSE );
        $adminForm->getElement ( 'password_confirm' )->setRequired ( FALSE );
        
        $mdlAdmins = new Model_Admin();
        
        if ($this->_request->isPost () && $adminForm->isValid ( $_POST )) {
            $data = $adminForm->getValues ();
            
            $currentAdmin = $mdlAdmins->getCurrentAdminInfor();
            if ($data ['username']) {
                $currentAdmin->username = $data ['username'];
                $currentAdmin->save ();
            }
            
            if ($data ['password']) {
                $currentAdmin->password = md5 ( $data ['password'] );
                $currentAdmin->save ();
            }
        } else {
            $currentAdmin = $mdlAdmins->getCurrentAdminInfor ()->toArray ();
            $adminForm->populate ( $currentAdmin );
        }
        
        $this->view->form = $adminForm;
    }

    public function loginAction() 
    {
    	$this->logoutAction();
    	
        $adminForm = new Form_Admin();
        $adminForm->setAction ( $this->_helper->url ( 'login', 'admin' ) );
        $adminForm->removeElement ( 'password_confirm' );
        
        if ($this->_request->isPost () && $adminForm->isValid ( $_POST ))
        {
            $data = $adminForm->getValues ();
            
            //set up the auth adapter
            //get the default db adapter
            $db = Zend_Db_Table::getDefaultAdapter ();
            
            //create the auth adapter
            $authAdapter = new Zend_Auth_Adapter_DbTable ( $db, 'admins', 'username', 'password' );
            
            //set the username and password
            $authAdapter->setIdentity ( $data ['username'] );
            $authAdapter->setCredential ( md5 ( $data ['password'] ) );
            
            //authenticate
            $result = $authAdapter->authenticate ();
            if ($result->isValid ()) {
                // store the username, first and last names of the user
                $auth = Zend_Auth::getInstance ();
                $storage = $auth->getStorage ();
                $object = $authAdapter->getResultRowObject ( array ('id', 'username'));
                $object->role = 'admin';
                $storage->write ( $object );
                return $this->_redirect ( 'admin/home' );
            } else {
                $this->view->loginMessage = "Sorry, your username or password was incorrect";
            }
        }
        $this->view->form = $adminForm;
    }

    public function logoutAction()
    {
        $authAdapter = Zend_Auth::getInstance ();
        $authAdapter->clearIdentity ();
    }
    
    /*
     * TIPS FUNCTIONS (MANAGE, INSERT, UPDATE, DELETE)
     */
    
	public function tipsAction()
    {
        // fetch the news paginator adapter
        $mdlTips = new Model_Tips();
        $paginator = $mdlTips->getPaginator ( null, array ("date DESC" ), $this->_request->getParam ( 'page', 1 ), 30 );
        
        $this->view->paginator = $paginator;
    }
    
	public function tipsCreateAction()
    {
        $frmTips = new Form_Tips();
		
        if ($this->getRequest ()->isPost ())
        {
			if ($frmTips->isValid ( $_POST ))
            {
                $match = $frmTips->getValue ( 'match' );			
                $tip = $frmTips->getValue ( 'tip' );			
                $date = $frmTips->getValue ( 'date' );			
                $type = $frmTips->getValue ( 'type' );			
                $result = $frmTips->getValue ( 'result' );			
                
                $mdlTips = new Model_Tips();
                $result = $mdlTips->createTip($match, $tip, $date, $type, $result);
                if ($result)
                {
                    return $this->_redirect ( 'admin/tips' );
                }
            }
        }
        $frmTips->setAction ( $this->_helper->url ( 'tips-create', 'admin' ) );
        $this->view->form = $frmTips;
    }
    
	public function tipsUpdateAction()
    {
        $frmTips = new Form_Tips();
        
        if ($this->getRequest ()->isPost ())
        {
            if ($frmTips->isValid ( $_POST ))
            {
                $id = $frmTips->getValue ( 'id' );
                $match = $frmTips->getValue ( 'match' );			
                $tip = $frmTips->getValue ( 'tip' );			
                $date = $frmTips->getValue ( 'date' );			
                $type = $frmTips->getValue ( 'type' );			
                $result = $frmTips->getValue ( 'result' );
                
                $mdlTips = new Model_Tips();
       			$result = $mdlTips->updateTip($id, $match, $tip, $date, $type, $result);
                
                return $this->_redirect ( 'admin/tips' );
            }
        }
        else
        {
            $id = $this->_request->getParam ( 'id' );
            
            $mdlTips = new Model_Tips();
            $currentTip = $mdlTips->find ( $id )->current ();
            $currentTip->date = date_create_from_format( 'Y-m-d H:i:s', $currentTip->date )->format('d-m-Y');
            $frmTips->populate ( $currentTip->toArray () );
        }
        $frmTips->setAction ( $this->_helper->url ( 'tips-update', 'admin' ) );
        $this->view->form = $frmTips;
    }

	public function tipsDeleteAction()
    {
        $id = $this->_request->getParam ( 'id' );
        $tipsModels = new Model_Tips();
		try {
			$tipsModels->deleteTip($id);
		} catch(Exception $ex) {
		}
        return $this->_redirect ( 'admin/tips' );
    }
    
    /*
     * MEMBERS FUNCTIONS (MANAGE, INSERT, UPDATE, DELETE)
     */
    
    public function membersAction()
    {
    	// fetch the news paginator adapter
        $mdlMembers = new Model_Members();
        $paginator = $mdlMembers->getPaginator ( null, array ("created_ts DESC" ), $this->_request->getParam ( 'page', 1 ), 30 );
        
        $this->view->paginator = $paginator;
    }
    
    /*
     * PAYMENTS FUNCTIONS (MANAGE)
     */
    
    public function paymentsAction()
    {
       
    }
}